<?php

namespace app\model\system;

use albert\services\upload\Upload;
use app\model\BaseModel;
use albert\traits\ModelTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Throwable;

/**
 * 附件表 Model
 */
class SystemAttachment extends BaseModel
{
    use ModelTrait;

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'att_id';

    /**
     * 初始化查询条件
     * Author: Albert3306
     * Date: 2021/7/1
     * @param $where
     * @param null $model
     * @return SystemAttachment|mixed
     */
    public static function initWhere($where, $model = null)
    {
        $model = $model ?? new self;
        if (isset($where['pid']) && $where['pid'] !== '') $model = $model->where('pid', $where['pid']);
        if (isset($where['att_id']) && $where['att_id'] !== '') $model = $model->where('att_id', $where['att_id']);
        if (isset($where['in_att_id']) && $where['in_att_id'] !== '') $model = $model->whereIn('att_id', $where['in_att_id']);
        if (isset($where['module_type']) && $where['module_type'] !== '') $model = $model->where('module_type', $where['module_type']);
        return $model;
    }

    /**
     * 添加附件记录
     * @param $name
     * @param $att_size
     * @param $att_type
     * @param $att_dir
     * @param string $satt_dir
     * @param int $pid
     * @param int $imageType
     * @param int $time
     * @param int $module_type
     * @return SystemAttachment
     */
    public static function attachmentAdd($name, $att_size, $att_type, $att_dir, string $satt_dir = '', int $pid = 0, int $imageType = 1, int $time = 0, int $module_type = 1): SystemAttachment
    {
        $data['name'] = $name;
        $data['att_dir'] = $att_dir;
        $data['satt_dir'] = $satt_dir;
        $data['att_size'] = $att_size;
        $data['att_type'] = $att_type;
        $data['image_type'] = $imageType;
        $data['module_type'] = $module_type;
        $data['time'] = $time ? $time : time();
        $data['pid'] = $pid;
        return self::create($data);
    }

    /**
     * 获取后台上传的图片列表
     * Author: Albert3306
     * Date: 2021/7/1
     * @param $where
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getImageList($where): array
    {
        $where['module_type'] = 1;
        $list = self::initWhere($where)->page((int)$where['page'], (int)$where['limit'])->order('att_id desc,time desc')->select();
        $list = count($list) ? $list->toArray() : [];
        $count = $where['pid'] ? self::initWhere($where)->count() : self::initWhere(['module_type' => 1])->count();
        return compact('list', 'count');
    }

    /**
     * 删除文件和数据
     * Author: Albert3306
     * Date: 2021/7/2
     * @param $att_id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function deleteImgAndData($att_id)
    {
        $attInfo = self::get($att_id);
        if ($attInfo) {
            try {
                $upload = new Upload((int)$attInfo['image_type']);
                if ($attInfo['image_type'] == 1) {
                    if (strpos($attInfo['att_dir'], '/') == 0) {
                        $attInfo['att_dir'] = substr($attInfo['att_dir'], 1);
                    }
                    $upload->delete($attInfo['att_dir']);
                } else {
                    $upload->delete($attInfo['name']);
                }
            } catch (Throwable $e) {
            }
            self::initWhere(['att_id' => $att_id])->delete();
        }
    }
}